#include <fstream>
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include "../../components/CmdLine/cmdargs.h"
#include "../../components/greta/regexpr2.h"
#include "get_arg.h"
using std::string;
using std::ofstream;
using std::ifstream;
using std::stringstream;
using std::cerr;
using std::cout;





//_______________________________________________________________________________________

//		GetArguments

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool GetArguments(int argc, char* argv[], t_program_args& args)
{
	// optional help directive
	CmdArgUsage opt_help('h', "help", 	"display this message", &cout);

	// required options
	const unsigned REQUIRED = CmdArg::isREQ | CmdArg::isVALREQ;
	CmdArgStr opt_dbname('d', "dbname", "<string>",
								"PostgreSQL database name. ", REQUIRED);
	CmdArgStr opt_host	('h', "host", "<string>",
								"PostgreSQL database host computer address. ");
	CmdArgStr opt_user	('u', "user", "<string>",
								"PostgreSQL database user name. Defaults to 'postgres'.");
	CmdArgStr opt_password('p', "password", "<string>",
								"PostgreSQL database password.");
	CmdArgStr opt_file_taxa_id	('i', "id_file", "FILE",
								"file location of NCBI 'gi_taxid_prot.dmp'",
								REQUIRED);
	CmdArgStr opt_file_taxa_names('n', "names_file", "FILE",
								"file location of NCBI 'names.dmp'",
								REQUIRED);
	CmdArgStr opt_file_taxa_nodes('tree', "tree_file", "FILE",
								"file location of NCBI taxonomy tree 'nodes.dmp'",
								REQUIRED);
	
	CmdArgStr opt_file_error('e', "error_file", "FILE",
								"will contain accessions which could not be processed.",
								REQUIRED);
	
	CmdArgSet opt_verbose		('v', "verbose",
								"warnsfor each line without taxid.");
	

	string exe_file_name =  boost::filesystem::path(*argv).leaf();
	
	// construct command line
	CmdLine  cmdline(exe_file_name.c_str(),
							&opt_dbname,
							&opt_host,
							&opt_user,
							&opt_password,
							&opt_file_taxa_id,
							&opt_file_taxa_names,
							&opt_file_error,
							&opt_verbose,
							&opt_help,
							0);
	cmdline.description( "This program enters NCBI Taxonomical Information into Panda.");
	// parse command line
	// report command line errors
	CmdArgvIter  argv_iter(--argc, ++argv);
	if (cmdline.parse(argv_iter))
		return false;

	args.verbose	= opt_verbose;


	args.file_taxa_id		= opt_file_taxa_id;
	args.file_taxa_names	= opt_file_taxa_names;
	args.file_taxa_nodes	= opt_file_taxa_nodes;
	args.file_error			= opt_file_error;
	
	// initialize postgresql login
	string dbname	= "'dbname=" +opt_dbname.str();
	string host		= opt_host;
	if (host.length())
		host = "host=" + host;
	string password = opt_password;
	if (password.length())
		password = "password=" + password;
	string user		= opt_user;
	if (!user.length())
		user = "postgres";
	user = "user=" + user;
	args.db_login = dbname + ' ' + host + ' ' + password + ' ' + user + "'";

	return true;
}

